<?php
namespace app\index\service;
use think\Db;
class  Pay{
    //订单支付
    function orderPayApi($o_no,$pay_type){
        $order = Db::name('order');
        $order_data = $order->where('o_no',$o_no)->field('o_money,o_state,o_type_id')->find();
        if(empty($order_data)){
            return  messageInfo ( '101', '订单不存在！' );
        }else if($order_data['o_state'] != 1){
            return  messageInfo ( '101', '订单已支付，请勿重复支付！' );
        }
        $amount = $order_data['o_money'] * 100;
        if($amount < 1) return  messageInfo ( '101', '订单金额不能小于0.01！' );
        $orderCode= date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        if($pay_type == 'wx_pub_qr'){
              $extra = array('product_id' =>$orderCode);
        }else if($pay_type == 'alipay_pc_direct'){
              $url = config('tempredirect_url').'user/myorder';
              $extra = array('success_url'=>$url);
        }else{
            return  messageInfo ( '101', '支付方式不存在！' );
        }
        if($order_data['o_type_id'] == 1){
            $title = '律师函';
        }else{
            $title = '企业劳动风险防范专项套餐';
        }
        $pay_array = array(
                'order_no'  => $orderCode,
                'amount'    => $amount,//订单总金额, 人民币单位：分（如订单总金额为 1 元，此处请填 100）
                'app'       => array('id' => 'app_mjXLiLWHSSaPPKyj'),
                'channel'   => $pay_type,
                'currency'  => 'cny',
                'extra'     => $extra,
                'client_ip' => get_client_ip_c(),
                'subject'   => $title,
                'body'      => $title
        );
        $a = $order->where('o_no',$o_no)->setField('o_ping_no',$orderCode);
        if(!$a){
            return  messageInfo ( '101', '支付失败！' );
        }
        return  messageInfo ( '202', '请求生成功',$pay_array) ;
    }

    //ping++异步
    function payhooksApi($o_third_id,$channel,$order_no){
        if(empty($o_third_id) || empty($channel) || empty($order_no)){
            return  false;
        }
        $order   = Db::name('order');
        $o_state = $order->where('o_ping_no',$order_no)->value('o_state');
        if($o_state != 1){
            return  true;
        }
        $data_arrar = array(
            'o_state'     => 2,
            'o_third_id'  => $o_third_id,
            'o_pay_type'  => $channel,
            'o_paytime'   => date('Y-m-d H:i:s')
        );
        $a = Db::name('order')->where('o_ping_no',$order_no)->update($data_arrar);
        if(!$a){
            return  false;
        }
        return true;
    }

}
